package earogya.pharmacy.patient.messages;

import java.io.IOException;
import java.sql.Time;

import org.hibernate.HibernateException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.datatype.EI;
import ca.uhn.hl7v2.model.v251.message.RDE_O11;
import earogya.pharmacy.patient.PatientHibernateManager;
import earogya.pharmacy.patient.Requested_Treatments;
import ca.uhn.hl7v2.model.v251.segment.ORC;
import ca.uhn.hl7v2.model.v251.segment.RXE;

/*RDE^O11 (pharmacy treatment encoded order) message handler
 * 
 * reads the RDE^O11 message and extract data from it and
 * sends the prescribed drug details to the requested_drugs database using hibernate
 * 
 * */
public class RDE_O11_Handler {
	

	private RDE_O11 adrMsg;
	private PatientHibernateManager hibernate_manager=new PatientHibernateManager();
	private Requested_Treatments request = new Requested_Treatments();
	public RDE_O11_Handler(RDE_O11 adrMsg){
		this.adrMsg=adrMsg;
	}
	
	public String manageMessage() throws HL7Exception, IOException{
		String ack = null;
		try{
				this.updateDrugRequestDetails();
				ack=adrMsg.generateACK().encode();
		}
		catch(HL7Exception e){
			ack = adrMsg.generateACK("OE", e).encode();
		}
		return ack;
	}
	
	public void updateDrugRequestDetails() throws HL7Exception, HibernateException{
		ORC orc = adrMsg.getORDER().getORC();
		RXE rxe = adrMsg.getORDER().getRXE();
		
		EI rId = orc.getOrc2_PlacerOrderNumber();
		request.setRequestId(Integer.parseInt(rId.getEi1_EntityIdentifier()
					.getValue()));
		if(adrMsg.getPATIENT().getPID().getPid2_PatientID().getCx1_IDNumber().getValue()!=null){
			request.setPatientId(Integer.parseInt(adrMsg.getPATIENT().getPID().getPatientID()
					.getCx1_IDNumber().getValue()));
		}
		if(rxe.getRxe2_GiveCode().getCe1_Identifier().getValue() != null){
			request.setDrugId(Integer.parseInt(rxe.getRxe2_GiveCode()
					.getCe1_Identifier().getValue()));
		}
		if (orc.getOrc7_QuantityTiming(0).getQuantity().getCq1_Quantity()
				.getValue() != null) {
			request.setQuantity(Integer.parseInt(orc.getOrc7_QuantityTiming(0).getQuantity().getCq1_Quantity().getValue()));
		}
		if (orc.getOrc10_EnteredBy(0).getIDNumber().getValue()!= null) {
			request.setPrescribedByID(Integer.parseInt(orc.getOrc10_EnteredBy(0).getIDNumber().getValue()));
		}
		if(rxe.getPrescriptionNumber().getValue() != null){
			request.setPrescriptionNo(Integer.parseInt(rxe.getPrescriptionNumber().getValue()));
		}
		if (orc.getDateTimeOfTransaction().getTime().getValueAsDate() != null) {
			request.setRequestDate(orc.getDateTimeOfTransaction().getTime()
					.getValueAsDate());
			request.setRequestTime(this.toTime());
		}
		if(rxe.getGiveAmountMaximum().getValue() != null){
			request.setMaxAmount(Integer.parseInt(rxe.getGiveAmountMaximum().getValue()));
		}
		if(rxe.getGiveAmountMinimum().getValue() != null){
			request.setMinAmount(Integer.parseInt(rxe.getGiveAmountMinimum().getValue()));
		}
		if(rxe.getGiveUnits().getCe1_Identifier().getValue() != null){
			request.setAmountUnits(rxe.getRxe5_GiveUnits().getCe1_Identifier().getValue());
		}
		if(rxe.getGiveDosageForm().getCe1_Identifier().getValue() != null){
			request.setDosage(rxe.getGiveDosageForm().getCe1_Identifier().getValue());
		}
		if(rxe.getSubstitutionStatus().getValue()=="Y"){
			request.setAllowSubstitutes(true);
		}else{
			request.setAllowSubstitutes(false);
		}
		hibernate_manager.saveObject(request);
	}
	
	@SuppressWarnings("deprecation")
	public Time toTime() throws DataTypeException {
		int hh = 0, mm = 0, ss = 0;
		ORC orc = adrMsg.getORDER().getORC();
		try {
			hh = orc.getDateTimeOfTransaction().getTime().getHour();
			mm = orc.getDateTimeOfTransaction().getTime().getMinute();
			ss = orc.getDateTimeOfTransaction().getTime().getSecond();
		} catch (DataTypeException e) {
		}
		Time time = new Time(hh, mm, ss);
		return time;

	}

}
